using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using Oplaco.Core.BusinessLogicLayer;
using System.Configuration;

public partial class MyProfile_aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            txtNickname.Text = Server.HtmlDecode(Profile.Nickname);
            txtFirstName.Text = Server.HtmlDecode(Profile.Firstname);
            txtLastName.Text = Server.HtmlDecode(Profile.Lastname);
            txtDelicious.Text = Server.HtmlDecode(Profile.Delicious);
            txtPhonenumberPrefix.Text = Profile.PhoneNumberPrefix.ToString();
            txtPhonenumber.Text = Profile.PhoneNumber.ToString();
            //txtTwitter.Text = Server.HtmlDecode(Profile.Twitter);
            txtZipcode.Text = Profile.ZipCode.ToString();
            txtZipcodeExtension.Text = Profile.ZipCodeExtension.ToString();
            txtFacebook.Text = Server.HtmlDecode(Profile.Facebook);
            txtEmail.Text = Membership.GetUser().Email;
        }
    }



    protected void SaveButton_Click(object sender, EventArgs e)
    {        
        Page.Validate("ChangeProfile");
        string avatarUrl = String.Empty;
        if (Page.IsValid)
        {
            try
            {
                MembersDB MDB = new MembersDB();
                MDB.UpdateProfile(Profile.Username, 
                        Server.HtmlEncode(txtNickname.Text),
                        Server.HtmlEncode(txtFirstName.Text),
                        Server.HtmlEncode(txtLastName.Text),
                        txtPhonenumberPrefix.Text,
                        txtPhonenumber.Text,
                        Int32.Parse(txtZipcode.Text),
                        Server.HtmlEncode(txtZipcodeExtension.Text),
                        avatarUrl,
                        String.Empty,
                        String.Empty,
                        Server.HtmlEncode(txtFacebook.Text),
                        Server.HtmlEncode(txtXbox.Text),
                        Server.HtmlEncode(txtDelicious.Text));

                this.BackToMyShop(true, 101);
            }
            catch (Oplaco.Exceptions.BusinessLayerException BLE)
            {
                CustomValidator val = new CustomValidator();
                val.IsValid = false;
                val.ErrorMessage = ConfigurationManager.AppSettings["GenericErrorMessage"];
                val.ValidationGroup = "LoginGroup";
                this.Page.Validators.Add(val);
                Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(BLE);
            }
            catch (Exception exception)
            {
                CustomValidator val = new CustomValidator();
                val.IsValid = false;
                val.ErrorMessage = ConfigurationManager.AppSettings["GenericErrorMessage"];
                val.ValidationGroup = "LoginGroup";
                this.Page.Validators.Add(val);
                
                Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(exception);
            }
        }
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        BackToMyShop(true, 0); ;
    }

    protected void BackToMyShop(bool success, int messageId)
    {
        Response.Redirect("~/MyShop/Default.aspx?s="+success+"&m=M"+messageId);
    }
   
    protected void FirstNameValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = false;
        if (Regex.IsMatch(txtFirstName.Text, @"^[0-9\-\p{L}\p{Zs}\p{Lu}\p{Ll}\']{1,40}$"))
        {
            args.IsValid = true;
        }
    }
    protected void LastNameValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = false;
        if (Regex.IsMatch(txtLastName.Text, @"^[0-9\-\p{L}\p{Zs}\p{Lu}\p{Ll}\']{1,40}$"))
        {
            args.IsValid = true;
        }
    }


}
